﻿using System.Xml;
using System.Xml.Linq;

namespace _45_C_如何插入_删除_修改xml文件
{
    internal class Program
    {
        static void Main(string[] args)
        {
            UpdateData();
            string path = "../../../Student.Xml";//当前运行的目录

            XmlDocument xmlDoc = new XmlDocument();
            
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            XmlReader xmlread = XmlReader.Create(path, settings);

            xmlDoc.Load(xmlread);

            XmlNode xmlNode = xmlDoc.SelectSingleNode("StudentInfo");
            XmlNodeList xmlNodeList = xmlNode.ChildNodes;//字节点
            List<Student> list = new List<Student>();

            foreach (XmlNode item in xmlNodeList)//将节点转换成文档
            {
                Student student = new Student();

                XmlElement xmlElement = (XmlElement)item;
                student.Grade = xmlElement.GetAttribute("Grade");
                student.Teacher = xmlElement.GetAttribute("Teacher");

                XmlNodeList xmlNodeList1 = xmlElement.ChildNodes;
                student.Name = xmlNodeList1.Item(0).InnerText;
                student.age = Convert.ToInt32(xmlNodeList1.Item(1).InnerText);

                list.Add(student);
            }

            list.ForEach(student => Console.WriteLine($"{student.Name}  {student.age}"));
        }

        //插入数据
        public static void InsertData()
        {
            string path = "../../../Student.Xml";//当前运行的目录

            XElement xe = XElement.Load(path);
            XElement student = new XElement("Student",
                new XAttribute("Grade", "二年级"),
                new XAttribute("Teacher", "ll"),
                new XElement("Name", "test"),
                new XElement("age", 10)
                );
            xe.Add(student);
            xe.Save(path);
        }

        //删除数据
        public static void DeleteData()
        {
            string path = "../../../Student.Xml";//当前运行的目录

            XElement xe = XElement.Load(path);

            var students = xe.Elements("Student");

            var students_1 = from student in students
                             where (string)student.Attribute("Teacher") == "张老师"
                             select student;

            var students_2 = xe.Elements("Student").Where(student => (string)student.Attribute("Teacher") == "张老师");

            if (students_2 != null && students_2.Count() > 0)
            {
                //students.First().Remove();
                //students.Remove();
            }

            xe.Save(path);
        }

        public static void UpdateData()
        {

            string path = "../../../Student.Xml";//当前运行的目录

            XElement xe = XElement.Load(path);

            var students_2 = xe.Elements("Student").Where(student => (string)student.Attribute("Teacher") == "张老师");

            if (students_2 != null && students_2.Count() > 0)
            {
                XElement xElement = students_2.First();
                //xElement.SetAttributeValue("Grade", "六年级");
                xElement.ReplaceNodes(
                                 //new XElement("Student",
                                 //             new XAttribute("Grade", "二年级"),
                                 //             new XAttribute("Teacher", "ll"),
                                 //             new XElement("Name", "test"),
                                 //             new XElement("age", 10))
                                 //);
                                 new XElement("Name", "test"),
                                 new XElement("age", 10));
            }
            xe.Save(path);
        }
    }
}
